<template>
	<!-- <div class="login" :style="{backgroundImage:'url('+loginPage.backgroundImg+')'}">
    <div class="el-login-top">
      <img :src="loginPage.titleImg" alt="">
    </div>
    <el-form ref="loginRef" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">{{loginPage.title}}</h3>
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          size="large"
          auto-complete="off"
          placeholder="账号"
        >
          <template #prefix><svg-icon icon-class="user" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          size="large"
          auto-complete="off"
          placeholder="密码"
          @keyup.enter="handleLogin"
        >
          <template #prefix><svg-icon icon-class="password" class="el-input__icon input-icon" /></template>
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          v-model="loginForm.code"
          size="large"
          auto-complete="off"
          placeholder="验证码"
          style="width: 63%"
          @keyup.enter="handleLogin"
        >
          <template #prefix><svg-icon icon-class="validCode" class="el-input__icon input-icon" /></template>
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img"/>
        </div>
      </el-form-item>
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="large"
          type="primary"
          style="width:100%;"
          @click.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div style="float: right;" v-if="register">
          <router-link class="link-type" :to="'/register'">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
    
    <div class="el-login-footer">
      <div >{{loginPage.pageFooter}}</div>
    </div>
  </div>
  <div>
    
  </div> -->
	<div class="login" :style="{ backgroundImage: 'url(' + loginPage.loginBac2 + ')' }">
		<!-- <div class="leftBox">
			<div class="left-box-logo">
        <br/> 信息管理平台
			</div>
			<div class="el-login-footer-two">
				<div>{{ loginPage.pageFooter }}</div>
			</div>
		</div> -->
		<div class="login-form-box">
			<div class="left-box-logo">
				<!-- <img :src="loginPage.leftImg" alt="" /> -->
				<br />
				信息管理平台
			</div>
			<el-form
				style="flex: 1.5"
				ref="loginRef"
				:model="loginForm"
				:rules="loginRules"
				class="login-form"
			>
				<h3 class="title">用户登录</h3>
				<!-- <div class="label_item">请输入账号</div> -->
				<el-form-item prop="username">
					<el-input
						v-model="loginForm.username"
						type="text"
						size="large"
						auto-complete="off"
						placeholder="请输入账号"
					>
						<template #prefix>
							<svg-icon icon-class="user" class="el-input__icon input-icon" />
							<span class="label-text">账号</span>
						</template>
					</el-input>
				</el-form-item>
				<!-- <div class="label_item">请输入密码</div> -->
				<el-form-item prop="password">
					<el-input
						v-model="loginForm.password"
						type="password"
						size="large"
						auto-complete="off"
						placeholder="请输入密码"
						@keyup.enter="handleLogin"
						style="border: none"
					>
						<template #prefix>
							<svg-icon icon-class="password" class="el-input__icon input-icon" />
							<span class="label-text">密码</span>
						</template>
					</el-input>
				</el-form-item>
				<!-- <div class="label_item">请输入验证码</div> -->
				<el-form-item prop="code" v-if="captchaEnabled">
					<el-input
						v-model="loginForm.code"
						size="large"
						auto-complete="off"
						placeholder="请输入验证码"
						style="width: 70%"
						@keyup.enter="handleLogin"
					>
						<template #prefix>
							<svg-icon icon-class="validCode" class="el-input__icon input-icon" />
							<span class="label-text">验证码</span>
						</template>
					</el-input>
					<div class="login-code" style="width: 26%">
						<img :src="codeUrl" @click="getCode" class="login-code-img" />
					</div>
				</el-form-item>
				<!-- <el-checkbox v-model="loginForm.rememberMe" style="margin: 0px 0px 25px 0px"
				>记住密码</el-checkbox
			> -->
				<el-form-item style="width: 100%; margin-bottom: 0%; transform: translateY(20px)">
					<el-button
						:loading="loading"
						size="large"
						type="primary"
						style="width: 100%"
						@click.prevent="handleLogin"
					>
						<span v-if="!loading">登 录</span>
						<span v-else>登 录 中...</span>
					</el-button>
					<div style="float: right" v-if="register">
						<router-link class="link-type" :to="'/register'">立即注册</router-link>
					</div>
				</el-form-item>
			</el-form>
		</div>
	</div>
</template>
// TODO  验证码样式
<script setup>
import { getCodeImg } from '@/api/login'
import Cookies from 'js-cookie'
import useUserStore from '@/store/modules/user'
import useSettingsStore from '@/store/modules/settings'
import { sm4Encrypt } from '@/utils/sm4'
const settingsStore = useSettingsStore()
const userStore = useUserStore()
const router = useRouter()
const { proxy } = getCurrentInstance()
const loginPage = computed(() => settingsStore.loginPage)
const loginForm = ref({
	username: '',
	password: '',
	rememberMe: false,
	//code: '',
	uuid: ''
})

const loginRules = {
	username: [{ required: true, trigger: 'blur', message: '请输入您的账号' }],
	password: [{ required: true, trigger: 'blur', message: '请输入您的密码' }]
	//code: [{ required: true, trigger: 'change', message: '请输入验证码' }]
}

const codeUrl = ref('')
const loading = ref(false)
// 验证码开关
const captchaEnabled = ref(false)
// 注册开关
const register = ref(false)
const redirect = ref(undefined)

function handleLogin() {
	proxy.$refs.loginRef.validate((valid) => {
		if (valid) {
			loading.value = true
			// 勾选了需要记住密码设置在 cookie 中设置记住用户名和密码
			if (loginForm.value.rememberMe) {
				Cookies.set('username', loginForm.value.username, { expires: 30 })
				Cookies.set('password', encrypt(loginForm.value.password), { expires: 30 })
				Cookies.set('rememberMe', loginForm.value.rememberMe, { expires: 30 })
			} else {
				// 否则移除
				Cookies.remove('username')
				Cookies.remove('password')
				Cookies.remove('rememberMe')
			}
			// 调用action的登录方法
			//js深拷贝写法
			//

			let curLoginForm = JSON.parse(JSON.stringify(loginForm.value))
			curLoginForm.password = sm4Encrypt(curLoginForm.password)
			curLoginForm.username = sm4Encrypt(curLoginForm.username)
			userStore
				.login(curLoginForm)
				.then(() => {
					router.push({ path: redirect.value || '/' })
				})
				.catch(() => {
					loading.value = false
					// 重新获取验证码
					if (captchaEnabled.value) {
						getCode()
					}
				})
		}
	})
}

function getCode() {
	getCodeImg().then((res) => {
		captchaEnabled.value = res.captchaEnabled === undefined ? true : res.captchaEnabled
		if (captchaEnabled.value) {
			codeUrl.value = 'data:image/gif;base64,' + res.img
			loginForm.value.uuid = res.uuid
		}
	})
}

function getCookie() {
	const username = Cookies.get('username')
	const password = Cookies.get('password')
	const rememberMe = Cookies.get('rememberMe')
	loginForm.value = {
		username: username === undefined ? loginForm.value.username : username,
		password: password === undefined ? loginForm.value.password : decrypt(password),
		rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
	}
}

getCode()
getCookie()
</script>

<style lang="scss" scoped>
.login {
	display: flex;
	justify-content: center;
	align-items: center;
	height: 100%;
	background-size: cover;
	--el-color-primary: #28C2B1;
	--el-color-primary-light-3: #28C2B1;
	--el-color-primary-dark-2: #28C2B1;
	--el-color-primary-light-5: #28C2B1;
}
.title {
	font-size: 24px;
	font-family: PingFangSC-Medium, PingFang SC;
	font-weight: 500;
	color: #28c2b1;
	line-height: 33px;
	margin: 0px auto 30px auto;
	text-align: center;
}
.el-login-top {
	height: 80px;
	line-height: 80px;
	line-height: 26px;
	position: absolute;
	top: 0px;
	width: 100%;
	background: #ffffff;
	// display: flex;
	img {
		position: absolute;
		top: 0;
		bottom: 0;
		margin: auto;
		margin-left: 46px;
	}
}
.login-form-box {
	position: absolute;
	left: 0;
	right: 0;
	top: 0;
	bottom: 0;
	margin: auto;
	width: 1030px;
	height: 60%;
	background: url('../assets/img/loginbox.png') no-repeat;
	background-size: 50% 100%;
	background-position: left top;
}
.label-text {
	margin-left: 20px;
	display: inline-block;
	width: 50px;
	text-align: left;
}
.login-form {
	// border-radius: 6px;
	// background: #ffffff;
	// width: 400px;
	padding: 6% 35px;
	box-sizing: border-box;
	display: flex;
	flex-direction: column;
	justify-content: space-around;
	border-radius: 6px;
	position: absolute;
	right: 0;
	height: 100%;
	background: #ffffff;
	width: 50%;
	// padding: 50px 25px 5px 25px;
	.el-input {
		height: 40px;
		input {
			height: 40px;
		}
	}
	.input-icon {
		height: 39px;
		width: 14px;
		margin-left: 0px;
	}
}
.login-tip {
	font-size: 13px;
	text-align: center;
	color: #bfbfbf;
}
.login-code {
	width: 33%;
	height: 40px;
	float: right;
	img {
		cursor: pointer;
		vertical-align: middle;
	}
}
.el-login-footer {
	background: #ffffff;
	height: 90px;
	padding: 20px 0;
	box-sizing: border-box;
	line-height: 30px;
	position: absolute;
	bottom: 0px;
	width: 100%;
	text-align: center;
	color: #657b9d;
	font-family: Arial;
	font-size: 12px;
	letter-spacing: 1px;
}
.login-code-img {
	height: 40px;
	padding-left: 12px;
}
// .leftBox {
// 	position: absolute;
// 	left: 6%;
// 	width: 22%;
// 	height: 100%;
// 	.left-box-logo {
// 		height: 30%;
// 		position: absolute;
// 		top: 0;
// 		bottom: 0;
// 		left: 0;
// 		right: 0;
// 		margin: auto;
//     font-size: 36px;
//     color: #ffffff;
//     text-align: center;
//     letter-spacing: 6px;
//     display: flex;
//     justify-content:center;
//     align-items: center;
// 		img {
// 			width: 100%;
// 			height: auto;
// 		}
// 	}
// 	.el-login-footer-two {
// 		position: absolute;
// 		bottom: 0;
// 		height: 90px;
// 		padding: 20px 0;
// 		box-sizing: border-box;
// 		line-height: 30px;
// 		position: absolute;
// 		bottom: 0px;
// 		width: 100%;
// 		text-align: center;
// 		color: #ffffff;
// 		font-family: Arial;
// 		font-size: 12px;
// 		letter-spacing: 1px;
// 	}
// }
.left-box-logo {
	position: absolute;
	left: 5%;
	top: 10%;
	font-size: 35px;
	font-family: PingFangSC-Semibold, PingFang SC;
	font-weight: 550;
	color: #ffffff;
	line-height: 60px;
	text-shadow: 0px 2px 4px rgba(9, 58, 51, 0.5);
}
.label_item {
	font-size: 16px;
	font-family: PingFangSC-Regular, PingFang SC;
	font-weight: 400;
	color: #3a4b5c;
	line-height: 22px;
	margin-bottom: 6px;
}
:deep(.el-input__wrapper) {
	box-shadow: 0px 1px var(--el-input-border-color, var(--el-border-color));
	border-radius: 0px;
}
:deep(.el-input__wrapper:hover) {
	box-shadow: 0px 1px var(--el-input-border-color, var(--el-border-color));
	border-radius: 0px;
}
:deep(.el-input__wrapper:focus) {
	box-shadow: 0px 1px var(--el-input-border-color, var(--el-border-color));
	border-radius: 0px;
}
:deep(.el-form-item--default) {
	margin-bottom: 36px;
}
</style>
